﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using WxYygh.Common;
using WxYygh.Models;
using WxYygh.ServiceReference1;

namespace WxYygh.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public string getksmc(string ksid)
        {

            List<GHKSModel> ksxl = new List<GHKSModel>();
            List<GHKSModel> ksdl = new List<GHKSModel>();
          

            if (Session["ksdl"] == null || Session["ksxl"] == null)
            {
                ZJSXHYYServiceSoapClient hydservice = new ZJSXHYYServiceSoapClient();
                string strXml = "<?xml version='1.0' encoding='utf-8'?>";
                strXml += "<data>";
                strXml += "<funcode>100101</funcode>";
                strXml += "<YYLY>3</YYLY>";
                strXml += "</data>";

                string outputXml = "";
                int refxml = hydservice.MainService(strXml, ref outputXml);

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(outputXml);
                XmlNodeList nodeList = xDoc.SelectSingleNode("data//list").ChildNodes;
                foreach (XmlNode node in nodeList)
                {
                    GHKSModel ModelList = new GHKSModel();
                    GHKSModel ksdlmodel = new GHKSModel();

                    XmlNodeList nodeList2 = node.ChildNodes;
                    foreach (XmlNode node2 in nodeList2)
                    {
                        switch (node2.Name)
                        {
                            case "GHKS":
                                ModelList.GHKS = int.Parse(node2.InnerText);
                                break;
                            case "KSDL":
                                ModelList.KSDL = node2.InnerText;
                                break;
                            case "KSDM":
                                ModelList.KSDM = node2.InnerText;
                                break;
                            case "KSMC":
                                ModelList.KSMC = node2.InnerText;
                                break;
                            case "KSMS":
                                ModelList.KSMS = node2.InnerText;
                                break;
                            default:
                                break;

                        }
                    }
                    ksxl.Add(ModelList);
                  
                }

                ksdl= ksxl.Where((x, i) => ksxl.FindIndex(z => z.GHKS == x.GHKS) == i).ToList().OrderBy(a=>a.GHKS).ToList(); 

              
                Session["ksdl"] = ksdl;
                Session["ksxl"] = ksxl;
            }


            JsonValues json = new JsonValues();
           
            if (string.IsNullOrEmpty(ksid)) {
                json.data = Session["ksdl"];

            } else {
                json.data = ((List<GHKSModel>)Session["ksxl"]).Where(x => x.GHKS == int.Parse(ksid));
            }

            return json.ToJson();
        }
    }
}